#!/bin/sh

# mvn release:prepare
# mvn release:perform
# would work better, but for some reason the svn commits fail
# for me through googlecode

BASE="https://stdf4j.googlecode.com/svn"
TRUNK="$BASE/trunk"
TAGS="$BASE/tags"

OUT=`svn st`
if [ ! -z "$OUT" ]
then
   echo Unresolved commits exist... exiting
   exit 1
else
   VERSION=`grep '<version>' pom.xml | sed -e 's/<\/*version>//g' | sed -e 's/ *//g'`
   RELEASE_VERSION=`echo $VERSION | sed -e 's/-SNAPSHOT//'`
   /bin/echo -n "Current version is $VERSION, release version [$RELEASE_VERSION]: "
   read input
   if [ ! -z "$input" ]
   then
      RELEASE_VERSION=$input
   fi

   DEV_REVISION=`echo $RELEASE_VERSION | awk -F. '{print $NF}' | xargs expr 1 +`
   DEV_VERSION=`echo $RELEASE_VERSION | sed -e "s/\(.*\.\).*/\1$DEV_REVISION-SNAPSHOT/"`
   /bin/echo -n "Next development version [$DEV_VERSION]: "
   read input
   if [ ! -z "$input" ]
   then
      DEV_VERSION=$input
   fi

   echo "Releasing version $RELEASE_VERSION"
   echo "With next development version $DEV_VERSION"

   for file in `find . -name 'pom.xml'`
   do
      sed -i -e "s/$VERSION/$RELEASE_VERSION/g" $file
   done

   svn ci -m "[release]: releasing version $RELEASE_VERSION"
   if [ $? -ne 0 ]
   then
      echo "Error checking in release"
      exit 1
   fi

   svn cp $TRUNK $TAGS/$RELEASE_VERSION -m "[release]: creating tag for $RELEASE_VERSION"
   if [ $? -ne 0 ]
   then
      echo "Error creating tag"
      exit 1
   fi

   for file in `find . -name 'pom.xml'`
   do
      sed -i -e "s/$RELEASE_VERSION/$DEV_VERSION/g" $file
   done

   svn ci -m "[release]: updating to next development version $DEV_VERSION"
   if [ $? -ne 0 ]
   then
      echo "Error updating to next development version"
      exit 1
   fi
fi
